package com.wanmait.hr.controller;

import com.wanmait.hr.service.DepartmentService;
import com.wanmait.hr.service.RegulationsService;
import com.wanmait.hr.service.UserService;
import com.wanmait.hr.vo.Department;
import com.wanmait.hr.vo.Regulations;
import com.wanmait.hr.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.beans.Transient;
import java.util.List;

/**
 * departmentController class
 *
 * @author 王志斌
 * @date 2019/01/22
 * */

@Controller
public class DepartmentController {
    @Resource
    private DepartmentService departmentService;
    @Resource
    private UserService userService;
    @Resource
    private RegulationsService regulationsService;

    /**部门管理--王志斌
     * @param model
     * */
    @RequestMapping("/manage/department/list")
    public void getList(Model model)
    {
        //获取所有的部门信息
        List<Department> getAllDepartmentList = departmentService.getAllDepartmentList();
        //获取部门总数
        Integer departNum = departmentService.departNum();

        model.addAttribute("departNum", departNum);
        model.addAttribute("departmentList", getAllDepartmentList);
    }

    /**查看部门备注
     * @param departmentId
     * */
    @RequestMapping("/manage/department/remark")
    public void getRemark(Integer departmentId, Model model)
    {
        String remark = departmentService.departRemark(departmentId);
        if(remark == null)
        {
            remark = "部门无备注";
        }

        model.addAttribute("departRemark",remark);
    }

    /**新增部门--王志斌
     * @return ModelAndView
     * */
    @RequestMapping("/manage/department/insert")
    public ModelAndView getInsert()
    {
        //下拉列表显示经理级管理员
        List<User> userList = userService.findAllSys();

        ModelAndView mv = new ModelAndView();
        mv.addObject("userList", userList);
        return mv;
    }

    /**新增部门--王志斌
     * @param department
     * @param html
     * @param session
     * */
    @RequestMapping(value = "/manage/department/insert", method = RequestMethod.POST)
    @ResponseBody
    public void getInsert(Department department, String html, HttpSession session)
    {
        //获取登陆的管理员信息
        //User user = (User)session.getAttribute("userinfo");

        User user = new User();
        user.setId(10);
        department.setUpAdmin(user);

        //新增部门_获取自增长的id
        int succeedInsert = departmentService.departmentInsert(department, html);
    }

    /**修改部门_王志斌
     * @param departId
     * @param model
     * */
    @RequestMapping("/manage/department/update")
    public void getUpdate(Integer departId, Model model)
    {
        //下拉列表显示经理级管理员
        List<User> userList = userService.findAllSys();
        //根据部门id查找部门信息
        Department department = departmentService.quartTheDepartById(departId);
        //根据部门id查找部门规章制度
        String departRegulation = regulationsService.queryTheRegulationByDepartId(departId);

        model.addAttribute("userList",userList);
        model.addAttribute("departInfo", department);
        model.addAttribute("departRegulation", departRegulation);
    }

    /**修改部门_王志斌
     * @param department
     * @param html
     * @param session
     * */
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping(value = "/manage/department/update", method = RequestMethod.POST)
    @ResponseBody
    public void getUpdate(Department department, String html, HttpSession session)
    {
        //获取登陆的管理员信息
        //User user = (User)session.getAttribute("userinfo");

        User user = new User();
        user.setId(10);

        //修改部门
        try {
            departmentService.updateDepart(department);
            //部门规章制度
            if(!html.equals(""))
            {
                regulationsService.updateDepartRegulation(html, department.getId(), user.getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**删除部门_王志斌
     * @param departId
     * */
    @RequestMapping(value = "/manage/department/delete", method = RequestMethod.POST)
    @ResponseBody
    public void getDelete(Integer departId)
    {
        departmentService.deleteDepart(departId);
    }

    @RequestMapping("/manage/addStaff/getDepartmentManager")
    @ResponseBody
    public String  getDepartmentManager(int id)//--陈_获取部门经理
    {
        String dmnName=departmentService.getDepartmentManager(id).getUser().getName();
        int leaderId=departmentService.getDepartmentManager(id).getUser().getId();
        dmnName=dmnName+","+leaderId;
        return dmnName;
    }
}
